package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.params.SaveCounterParams;
import com.ruoyi.system.domain.vo.CounterEditInfoVo;
import com.ruoyi.system.domain.vo.WhCounterListVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.WhCounter;
import com.ruoyi.system.service.IWhCounterService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货柜Controller
 *
 * @author ruoyi
 * @date 2023-03-07
 */
@RestController
@RequestMapping("/system/counter")
public class WhCounterController extends BaseController {
    @Resource
    private IWhCounterService whCounterService;

    /**
     * 查询货柜列表
     */
    @PreAuthorize("@ss.hasPermi('system:counter:list')")
    @GetMapping("/list")
    public TableDataInfo list(WhCounter whCounter) {
        startPage();
        List<WhCounterListVo> list = whCounterService.selectWhCounterList(whCounter);
        return getDataTable(list);
    }

    /**
     * 导出货柜列表
     */
    @PreAuthorize("@ss.hasPermi('system:counter:export')")
    @Log(title = "货柜", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WhCounter whCounter) {
        List<WhCounterListVo> list = whCounterService.selectWhCounterList(whCounter);
        ExcelUtil<WhCounterListVo> util = new ExcelUtil<WhCounterListVo>(WhCounterListVo.class);
        util.exportExcel(response, list, "货柜数据");
    }

    /**
     * 获取货柜详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:counter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(whCounterService.selectWhCounterById(id));
    }

    /**
     * 新增货柜
     */
    @PreAuthorize("@ss.hasPermi('system:counter:add')")
    @Log(title = "货柜", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SaveCounterParams params) {
        params.setCreateBy(getUserId());
        params.setCreateTime(new Date());
        return toAjax(whCounterService.saveCounter(params));
    }

    /**
     * 修改货柜
     */
    @PreAuthorize("@ss.hasPermi('system:counter:edit')")
    @Log(title = "货柜", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SaveCounterParams params) {
        params.setUpdateBy(getUserId());
        return toAjax(whCounterService.updateWhCounter(params));
    }

    /**
     * 删除货柜
     */
    @PreAuthorize("@ss.hasPermi('system:counter:remove')")
    @Log(title = "货柜", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(whCounterService.deleteWhCounterByIds(ids));
    }

    /**
     * 获取货柜编辑详情
     * @param id 货柜id
     * @return
     */
    @GetMapping("/edit/info/{id}")
    public AjaxResult getCounterEditInfo(@PathVariable("id") Integer id) {
        CounterEditInfoVo counterEditInfo = whCounterService.getCounterEditInfo(id);
        if (counterEditInfo == null) {
            throw new ServiceException("找不到货柜信息");
        }
        return success(counterEditInfo);
    }
}
